package msu.erdc.hci;

import msu.erdc.hci.customObjects.AccidentObject;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class AccidentHelperActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar bar = getActionBar();
        //bar.setDisplayShowTitleEnabled(false);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.addTab(bar.newTab()
        		.setText("Contacts")
        		.setTabListener(new TabListener<Contacts>(this, "contacts", Contacts.class)));
        bar.addTab(bar.newTab()
        		.setText("My Info")
        		.setTabListener(new TabListener<MyInfo>(this, "myInfo", MyInfo.class)));
        bar.addTab(bar.newTab()
        		.setText("Accident")
        		.setTabListener(new TabListener<AccidentList>(this, "accident", AccidentList.class)));
        
        if (savedInstanceState != null) {
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {

        case R.id.send:
        	ListAdapter adapter = new ArrayAdapter<AccidentObject>(AccidentHelperActivity.this, 
        															android.R.layout.simple_list_item_1, 
        															((Global)AccidentHelperActivity.this.getApplication()).accidents);//i am accessing the 'singleton' on this line and then getting accidents out of it
        	new AlertDialog.Builder(AccidentHelperActivity.this)
        	.setAdapter(adapter, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
					//emailIntent.setType("text/plain");
					emailIntent.setType("message/rfc822");
					emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Accident Info");
					emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Lots-o accident info here");
					startActivity(Intent.createChooser(emailIntent, "Send your email in:"));  
				}})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					//do nothing
				}})
			.setTitle("Choose accident to send")
			.setIcon(android.R.drawable.ic_dialog_info)
			.show();
            return true;
        }
        return false;
    }
    
    public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        private final Bundle mArgs;
        private Fragment mFragment;

        public TabListener(Activity activity, String tag, Class<T> clz) {
            this(activity, tag, clz, null);
        }

        public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
            mArgs = args;

            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null && !mFragment.isDetached()) {
                FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
            	//this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
            	FragmentManager man = mActivity.getFragmentManager();
            	if(man.getBackStackEntryCount()>0)
            		man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
            	//end
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        	FragmentManager man = mActivity.getFragmentManager();
        	if(man.getBackStackEntryCount()>0)
        		man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
}